package com.peycard.business;

import java.util.HashMap;
import java.util.Map;

import javax.ejb.Stateless;

import com.peycard.business.local.TagParserBeanLocal;
import com.peycard.common.Tag;
import com.peycard.common.util.Constantes;
import com.peycard.common.util.ReaderTagList;
import com.peycard.common.util.TagList;

/**
 * Session Bean implementation class TagParserBean
 */
@Stateless
public class TagParserBean implements TagParserBeanLocal {

	/**
	 * Default constructor.
	 */
	public TagParserBean() {
		// TODO Auto-generated constructor stub
	}
	
	

	/* (sin Javadoc)
	 * @see com.peycard.business.local.TagParserBeanLocal#parseProprietaryTags(java.lang.String)
	 */
	@Override
	public Map<ReaderTagList, Tag> parseProprietaryTags(String proprietaryTags){
		Map<ReaderTagList, Tag>  tagMap = new HashMap<ReaderTagList, Tag>();
		String sb = proprietaryTags;
		while(sb.length() > 6){
			String first2Carac = sb.substring(0, 2);
			first2Carac = first2Carac.toUpperCase();
			Tag myNewTag = new Tag();
			for (ReaderTagList tag : ReaderTagList.values()) {
				if (first2Carac.equals(tag.getName())) {
					myNewTag.setTagName(tag.getName());
					if(tag.getTlvToken() == 0){
						myNewTag.setTagName(tag.getName());
						myNewTag.setLengthHex(sb.substring(2, 4));
						myNewTag.setValue(sb.substring(4,
								myNewTag.getLengthDecimal() + 4));
						tagMap.put(tag, myNewTag);
						System.out.println(myNewTag);
						sb = sb.substring(myNewTag.getLengthDecimal() + 4);
					}else{
						/* para que sirve el digito 3 */
						String myHexLength = sb.substring(3,4);
						int myHexLengthInt = Integer.parseInt(myHexLength);
						String hexLength = sb.substring(4, 4+(myHexLengthInt*2));
						myNewTag.setLengthHex(hexLength);
						int begin = 4 + (myHexLengthInt * 2);
						int end = myNewTag.getLengthDecimal() + begin;
						myNewTag.setValue(sb.substring(begin, end));
						tagMap.put(tag, myNewTag);
						System.out.println(myNewTag);
						sb = sb.substring(myNewTag.getLengthDecimal() + begin);
					}
					
					break;
				}
			}
		}
		
		
		return tagMap;
	}
	
	/* (sin Javadoc)
	 * @see com.peycard.business.local.TagParserBeanLocal#parsePlainTagText(java.lang.String)
	 */
	@Override
	public Map<TagList, Tag> parsePlainTagText(String plainText) {
		/* Se declara la variable donde se almacenan los resultados */
		Map<TagList, Tag> tagMap = new HashMap<TagList, Tag>();
		/* Para poder mutar sin problemas este string se declara en uno nuevo */
		String sb = firstCleanTLV(plainText);
		/* Mientras el contenido sae mayor a 4 (2 tag , 2 length y 2 value) */
		while (sb.length() > 6) {
			/* Se evaluara primero si coincide con un tag de 4 caracteres */
			String first4Charac = sb.substring(0, 4);
			Tag myNewTag = new Tag();
			/* Se pone a falso la bandera */
			boolean is4CaracFound = false;
			/* Recorre la lista de TAGs que conocemos */
			for (TagList tag : TagList.values()) {
				/* Evalua si coincide con los primeros 4 caracteres */
				if (first4Charac.equals(tag.getName())) {
					/* Si coincide arma el objeto tipo TAG */
					myNewTag.setTagName(tag.getName());
					/*
					 * Settea el long de 2 caracteres en Hexa, al settear este
					 * se calcula en automatico el decimal
					 */
					myNewTag.setLengthHex(sb.substring(4, 6));
					/*
					 * Se settea el valor comenzando en 8 y terminando en length
					 * + 8 -1
					 */
					myNewTag.setValue(sb.substring(6,
							myNewTag.getLengthDecimal() + 6));
					is4CaracFound = true;
					tagMap.put(tag, myNewTag);
					System.out.println(myNewTag);
					sb = sb.substring(myNewTag.getLengthDecimal() + 6);
					break;
				}//Fin de if -> found 4 carac
			}//Fin de for
			/* Si no se encuentra en base a 4 carac. se busca por los primeros 2 */
			if (!is4CaracFound) {
				String first2Carac = first4Charac.substring(0, 2);
				for (TagList tag : TagList.values()) {
					if (first2Carac.equals(tag.getName())) {
						myNewTag.setTagName(tag.getName());
						myNewTag.setLengthHex(sb.substring(2, 4));
						myNewTag.setValue(sb.substring(4,
								myNewTag.getLengthDecimal() + 4));
						tagMap.put(tag, myNewTag);
						System.out.println(myNewTag);
						sb = sb.substring(myNewTag.getLengthDecimal() + 4);
						break;
					}
				}
			}//Fin de if -> 4 charac not found
			if(tagMap.size() == Constantes.MAX_EXPECTED_TAGS_FROM_READER){
				break;
			}
		}//Fin de while
		return tagMap;
	}
	
	/**
	 * Limpia el String oringinal removiendo los primeros 8 caracteres de un TAG propietario del reader
	 * @param tlv
	 * @return
	 */
	private String firstCleanTLV(String tlv){
		String myNewString = tlv.substring(8);
		return myNewString;
	}

}
